package controllers

import (
	"github.com/kataras/iris"
	"github.com/kataras/iris/mvc"
	"tizhi_front/model"
	"tizhi_front/nspel"
	"tizhi_front/request"
	"tizhi_front/response"
	"tizhi_front/utils"
)

type ConfigController struct {
	Ctx iris.Context
}

func (c *ConfigController) BeforeActivation(b mvc.BeforeActivation) {
	b.Handle("POST", "/video_url", "GetVideoUrl", beforeVideoUrl)
	b.Handle("POST", "/index_pic", "GetIndexPic", beforeCommonToken)
	b.Handle("POST", "/help_info", "GetHelpInfo", beforeCommonToken)
	b.Handle("GET", "/user_agreement", "GetUserAgreement")
}

func beforeVideoUrl(ctx iris.Context) {
	req := request.VideoUrl{}
	err := ctx.ReadJSON(&req)
	if err != nil {
		_, _ = ctx.JSON(response.InvalidParameters)
		return
	}
	if !utils.ValidateToken(&req.TokenInfo) {
		ctx.StatusCode(403)
		_, _ = ctx.JSON(response.InvalidParameters)
		return
	}
	ctx.Values().Set("uid", req.TokenInfo.Uid)
	ctx.Values().Set("data", req.VideoKey)
	ctx.Next()
}

// @Title get video url
// @Description 根据video key 获取url
// @Tags config
// @Param	body body request.VideoUrl	true "token"
// @Success 0	{object} response.JsonResponse
// @Failure 1 {string} string "用户未登录或者登陆已过期"
// @Failure 2 {string} string "系统错误，获取失败，具体error信息为系统报错信息"
// @router /config/video_url [post]
func (c *ConfigController) GetVideoUrl() response.JsonResponse {
	videoKey := c.Ctx.Values().Get("data").([]string)
	var err error
	for i := 0; i < len(videoKey); i++ {
		videoKey[i], err = nspel.GetDownloadSignUrl(videoKey[i])
		if err != nil {
			nspel.Error(err)
			return response.JsonResponse{
				ErrCode: 2,
				ErrInfo: err.Error(),
				Data:    "",
			}
		}
	}
	return response.JsonResponse{
		ErrCode: 0,
		ErrInfo: "",
		Data:    videoKey,
	}
}

// @Title get index page pic
// @Description 获取首页图片地址
// @Tags config
// @Param	body body response.TokenInfo true "token"
// @Success 0	{object} response.JsonResponse
// @Failure 1 {string} string "用户未登录或者登陆已过期"
// @Failure 2 {string} string "系统错误，获取失败，具体error信息为系统报错信息"
// @router /config/index_pic [post]
func (c *ConfigController) GetIndexPic() response.JsonResponse {
	config := model.Config{}
	err := config.Select()
	if err != nil {
		nspel.Error(err)
		return response.JsonResponse{
			ErrCode: 2,
			ErrInfo: err.Error(),
			Data:    "",
		}
	}
	return response.JsonResponse{
		ErrCode: 0,
		ErrInfo: "",
		Data:    config.IndexPic,
	}
}

// @Title get help info
// @Description 获取帮助与反馈信息
// @Tags config
// @Param	body body response.TokenInfo true "token"
// @Success 0	{object}	response.HelpInfoList
// @Failure 1 {string} string "用户未登录或者登陆已过期"
// @Failure 2 {string} string "系统错误，获取失败，具体error信息为系统报错信息"
// @router /config/help_info [post]
func (c *ConfigController) GetHelpInfo() response.JsonResponse {
	help := model.HelpE{}
	helps, err := help.SelectAllOrderByOrderAsc()
	if err != nil {
		nspel.Error(err)
		return response.JsonResponse{
			ErrCode: 2,
			ErrInfo: err.Error(),
			Data:    "",
		}
	}
	res := response.HelpInfoList{}
	res.HelpInfo = make([]response.HelpInfo, len(helps))
	for i := 0; i < len(helps); i++ {
		res.HelpInfo[i] = response.HelpInfo{
			Title:   helps[i].Question,
			Content: helps[i].Answer,
		}
	}
	return response.JsonResponse{
		ErrCode: 0,
		ErrInfo: "",
		Data:    res,
	}
}

// @Title get user agreement
// @Description 获取用户协议
// @Tags config
// @router /config/user_agreement [get]
func (c *ConfigController) GetUserAgreement() {
	c.Ctx.ServeFile("./config/user_agreement.html", true)
}
